import { Injectable } from '@angular/core';
import {
  CanActivate, Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot
} from '@angular/router';
import {AuthService} from './auth.service';

@Injectable()
export class AuthGuardCompanyService implements CanActivate {
  constructor(private router: Router, private authService: AuthService) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let url: string = state.url;

    return this.checkIsCompany(url);
  }
  // bug记录守卫成功路由会让authService.isLogin变成false
  checkIsCompany(url: string): boolean {
    if (this.authService.isCompany) { return true; }

    // Store the attempted URL for redirecting
    sessionStorage.setItem('redirectUrl', 'url');

    // Navigate to the login page with extras
    this.router.navigate(['/admin']);
    return false;
  }
}
